// pages/main-video/main-video.js
import { getTopMV } from '../../services/video'

Page({
  data: {
    videoList: [],
    offset: 0,
    hasMore: true
  },
 
  onLoad(options) {
    this.fetchTopMV();
  },

  // =================  发送网络请求的方法 =================  
  async fetchTopMV() {
    const res = await getTopMV(this.data.offset)

    const newVideoList = [...this.data.videoList, ...res.data]

    this.setData({ videoList: newVideoList })
    this.data.offset = this.data.videoList.length
    this.data.hasMore = res.hasMore
  },

  // =================  监听上拉和下拉功能  =================  
  onReachBottom() {
    // 1.判断是否有更多的数据
    if (!this.data.hasMore) return

    // 2.如果有更多的数据, 再请求新的数据
    this.fetchTopMV()
  },

  // =================  监听下拉刷新功能  =================
  async onPullDownRefresh() {
    // 1.清空之前的数据
    this.setData({ videoList: [] })
    this.data.offset = 0
    this.data.hasMore = true

    // 2.重新请求新的数据
    await this.fetchTopMV();

    // 3.停止下拉刷新
    wx.stopPullDownRefresh();
  }
})